#region File Header
//-----------------------------------------------------------------------------
// SibLib
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SibLib.Render
{    
    public class BasicModel
    {
        private string m_Name;
        private Model m_Model;
        private Matrix m_RotationMatrix;
        public Vector3 Position
        {
            get;
            set;
        }
        public SceneInfo SceneInfo
        {
            get;
            set;
        }
        public Color Color
        {
            get;
            set;
        }
        public float Scale
        {
            get;
            set;
        }


        public BasicModel(string name)
        {
            m_Name = name;
            m_RotationMatrix = Matrix.CreateRotationX(0);
            Position = Vector3.Zero;
            SceneInfo = null;
            Scale = 1.0f;
        }

        public void LoadContent(ContentManager content)
        {
            m_Model = content.Load<Model>(m_Name);
        }

        public void Draw(float timeFactor)
        {
            if (SceneInfo == null)
                return;

            Matrix viewMatrix = SceneInfo.ViewMatrix;
            Matrix projectionMatrix = SceneInfo.ProjectionMatrix;

            // Copy any parent transforms.
            Matrix[] transforms = new Matrix[m_Model.Bones.Count];
            m_Model.CopyAbsoluteBoneTransformsTo(transforms);

            Matrix translationMatrix = Matrix.CreateTranslation(Position);  // TODO: link to GP object.
            Matrix scaleMatrix = Matrix.CreateScale(Scale);                 // TODO: cache this

            // Draw the model. A model can have multiple meshes, so loop.
            foreach (ModelMesh mesh in m_Model.Meshes)                      // TODO: CHECK THIS FOR GARBAGE
            {
                // This is where the mesh orientation is set, as well as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)                // TODO: CHECK THIS FOR GARBAGE
                {
                    effect.EnableDefaultLighting();
                    effect.World = /*transforms[mesh.ParentBone.Index] * */m_RotationMatrix * scaleMatrix * translationMatrix;      // TODO: cache this!
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;
                    effect.LightingEnabled = true;
                    // TODO: Have a thought about coloring, emissiveness, etc.
                    effect.DiffuseColor = Color.ToVector3();                    
                }
                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
        }
    }
}